<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);
use app\admin\Controller;
use think\Db;
use think\Loader;
use think\Config;
use think\exception\HttpException;
/*
百度AI身份证识别功能
*/
class Baidu extends Controller
{
    use \app\admin\traits\controller\Controller;
    protected static $blacklist = [];
	protected $url = 'https://aip.baidubce.com/oauth/2.0/token';
    protected function filter(&$map)
    {
        if ($this->request->param('name')) {
            $map['title'] = ["like", "%" . $this->request->param('name') . "%"];
		  
        }
         $map['status']=['>=',0];
         //$map['cat_id']=array('neq',6);
    }
	/*向百度请求access token*/
	protected function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  //跳过SSL证书检查。这里使用https,鉴于车管业务不使用网络服务器，而在本地运行，在此选择跳过安全检查
        $data = curl_exec($curl);//运行curl
        curl_close($curl);
        return $data;
    }
	protected function get_access_token(){ //获取access_token
		$post_data['grant_type']    = 'client_credentials';  //百度接口固定值
		$post_data['client_id']     = 'RMQx1SVzAyCUkW1sLLldhdRy';	//百度开发者中心里的Api Key
		$post_data['client_secret'] = 'WG2HlwBh7vGA37HrHeVUPR6UcrvVptgZ';	//百度开发者中心里的Secret Key
		$o = "";
		foreach ($post_data as $k => $v ) 
		{
			$o.= "$k=" . urlencode( $v ). "&" ;
		}
		$post_data = substr($o,0,-1);
		$res = $this->request_post($this->url, $post_data);
		$res=json_decode($res,true);
		//dump($res);
		return $res['access_token'];
	}
	/**
	 * 发起http post请求(REST API), 并获取REST请求的结果
	 * @param string $url
	 * @param string $param
	 * @return - http response body if succeeds, else false.
	 */
	protected function post_info($url = '', $param = '')  //提交身份证图片到百度接口上获取数据
	{
		if (empty($url) || empty($param)) {
			return false;
		}
		$postUrl = $url;
		$curlPost = $param;
		// 初始化curl
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $postUrl);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		// 要求结果为字符串且输出到屏幕上
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,10);//最大链接时间，单位秒
		curl_setopt($curl, CURLOPT_TIMEOUT,32);   //最大执行时间，单位秒
		// post提交方式
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		// 运行curl
		// 检查是否有错误发生
		try{
			$data = curl_exec($curl);
		}
		catch(Exception $e){ 
			return $data['err']="50";
		}
		if(curl_errno($curl))
		{
			$data['err']=curl_error($curl);
			$data=json_encode($data);
			curl_close($curl);
			return $data;
		}else
		{
			curl_close($curl);
			return $data;
		}
	}
	public function get_idcar_info($name){
		$token = $this->get_access_token();  //获取access_token
		$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' . $token;
		$img = file_get_contents($name);
		$img = base64_encode($img);
		$bodys = array(
			"image" => $img,
			"id_card_side"=>'front'
		);
		$res = $this->post_info($url, $bodys);
		echo $res;
	}
	public function get_idcar_info_by_base64($data){
		$token = $this->get_access_token();  //获取access_token
		$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' . $token;
		$bodys = array(
			"image" => $data,
			"id_card_side"=>'front'
		);
		$res = $this->post_info($url, $bodys);
		echo $res;
	}
}
?>